package com.zyz.N_反射机制.基本应用.获取运行时类的完整结构;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;

/**
 * @author: vick
 * @Date: 2025/4/14
 * @Description:
 * @version: 1.0
 */
public class Test1 {
    public static void main(String[] args) throws NoSuchFieldException {
        Class<User1> userClass = User1.class;
        //获取到运行时类本身及其所有的父类中声明为public权限的属性
        /*Field[] fields = userClass.getFields();
        for (Field field : fields) {
            System.out.println(field);
        }*/

        //获取当前运行时类中声明的所有属性
        Field[] fields = userClass.getDeclaredFields();
        for (Field field : fields) {
            //1. 获取属性的权限修饰符
            /*
            *
            public static final int PUBLIC          = 0x00000001;
            public static final int PRIVATE          = 0x00000002;
            public static final int PROTECTED        = 0x00000004;
            public static final int STATIC           = 0x00000008;
            public static final int FINAL            = 0x00000010;
            * */
            int modifiers = field.getModifiers();
            System.out.print(Modifier.toString(modifiers) + "\t");

            //2. 数据类型
            Class<?> type = field.getType();
            System.out.print(type.getName() + "\t");

            //3. 变量名
            String name = field.getName();
            System.out.print(name + "\t");

            System.out.println();
        }
    }
}
